import cv2
import numpy as np

# 读取图像
image_path = '1.jpg'
image = cv2.imread(image_path)

# 显示图像的像素值
print(image)


# BGR ➔ RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# BGR ➔ GRAY
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# BGR ➔ HSV
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# 保存转换后的图像
cv2.imwrite('olympic_rings_rgb.jpg', image_rgb)
cv2.imwrite('olympic_rings_gray.jpg', image_gray)
cv2.imwrite('olympic_rings_hsv.jpg', image_hsv)


# 拆分 HSV 图像的通道
h_channel, s_channel, v_channel = cv2.split(image_hsv)

# 合并通道为 HSV 图像
hsv_merged = cv2.merge((h_channel, s_channel, v_channel))

# 保存拆分和合并后的图像
cv2.imwrite('h_channel.jpg', h_channel)
cv2.imwrite('s_channel.jpg', s_channel)
cv2.imwrite('v_channel.jpg', v_channel)
cv2.imwrite('hsv_merged.jpg', hsv_merged)

import cv2
import numpy as np
import matplotlib.pyplot as plt


def add_salt_pepper_noise(image, salt_prob, pepper_prob):
    # 创建噪声图像，初始化为原图像的拷贝
    noisy_image = np.copy(image)

    # 获取图像的高度、宽度和通道数
    h, w, c = image.shape

    # 生成椒盐噪声
    # 添加盐噪声 (白色)
    num_salt = np.ceil(salt_prob * h * w)
    coords_salt = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape[:2]]
    noisy_image[coords_salt[0], coords_salt[1], :] = 255

    # 添加椒噪声 (黑色)
    num_pepper = np.ceil(pepper_prob * h * w)
    coords_pepper = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape[:2]]
    noisy_image[coords_pepper[0], coords_pepper[1], :] = 0

    return noisy_image


# 读取图像 (你可以用自己的路径替换 'image_path')
image = cv2.imread('1.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # 转换为 RGB 格式以便于 matplotlib 显示

# 添加椒盐噪声，盐噪声概率为 1%，椒噪声概率为 1%
noisy_image = add_salt_pepper_noise(image, salt_prob=0.01, pepper_prob=0.01)

cv2.imwrite('noisy_image.jpg', noisy_image)


#九宫格

image1 = cv2.imread('1.jpg')
image2 = cv2.imread('2.jpg')
image3 = cv2.imread('3.jpg')
image4 = cv2.imread('4.jpg')
image5 = cv2.imread('5.jpg')
image6 = cv2.imread('6.jpg')
image7 = cv2.imread('7.jpg')
image8 = cv2.imread('8.jpg')
image9 = cv2.imread('9.jpg')


png_1 = np.hstack((image1,image2,image3))
png_2 = np.hstack((image4,image5,image6))
png_3 = np.hstack((image7,image8,image9))
jpg_1 = np.vstack((png_1,png_2,png_3))

cv2.imwrite('nine.jpg',jpg_1)


import numpy as np
import cv2

# 创建一个大小为200x200的空白图像（初始全为白色，值为255）
image = np.ones((200, 200), dtype=np.uint8) * 255

# 设置条纹的宽度（例如每条黑白条纹宽度为 20 像素）
stripe_width = 20

# 绘制黑色条纹
for i in range(0, 200, 2 * stripe_width):  # 每隔 40 个像素画一条黑色条纹
    image[:, i:i + stripe_width] = 0

# 保存生成的图像
cv2.imwrite('black_white_stripes.jpg', image)

import cv2
import numpy as np

# 创建一个白底的图像
image = np.ones((400, 600, 3), dtype=np.uint8) * 255

# 定义五环的中心位置和颜色（BGR格式）
colors = [(255, 0, 0), (0, 0, 0), (0, 0, 255), (0, 255, 255), (0, 255, 0)]  # 蓝、黄、黑、绿、红
centers = [(150, 200), (250, 200), (350, 200), (200, 270), (300, 270)]  # 五个环的中心位置

# 绘制五个圆
for i in range(5):
    cv2.circle(image, centers[i], 70, colors[i], 5)  # 圆心，半径，颜色，线宽

# 保存和展示图像
cv2.imwrite('olympic_rings.jpg', image)


import cv2
import numpy as np

# 创建白色背景图像
image1 = np.ones((400, 400, 3), dtype=np.uint8) * 255

# 定义五角星的顶点坐标 (手动计算的相对坐标)
def draw_star(image, center, size, color):
    pts = np.array([
        [center[0], center[1] - size],  # Top point
        [center[0] + size * 0.2245, center[1] - size * 0.309],  # Right top point
        [center[0] + size, center[1] - size * 0.309],  # Right point
        [center[0] + size * 0.363, center[1] + size * 0.118],  # Right bottom point
        [center[0] + size * 0.5878, center[1] + size],  # Bottom right point
        [center[0], center[1] + size * 0.382],  # Bottom point
        [center[0] - size * 0.5878, center[1] + size],  # Bottom left point
        [center[0] - size * 0.363, center[1] + size * 0.118],  # Left bottom point
        [center[0] - size, center[1] - size * 0.309],  # Left point
        [center[0] - size * 0.2245, center[1] - size * 0.309],  # Left top point
    ], np.int32)

    cv2.polylines(image, [pts], isClosed=True, color=color, thickness=3)

# 在不同位置绘制五个五角星
positions = [(80, 80), (240, 80), (80, 240), (240, 240), (160, 160)]
for pos in positions:
    draw_star(image1, pos, 40, (0, 255, 255))  # 绘制黄线不填充的五角星


cv2.imwrite('five_star_outline.jpg', image1)


# 创建浅灰底图像
image2 = np.ones((400, 400, 3), dtype=np.uint8) * 200  # 浅灰底

# 在不同位置绘制五个实心红色五角星
for pos in positions:
    pts = np.array([
        [pos[0], pos[1] - 40],  # Top point
        [pos[0] + 40 * 0.2245, pos[1] - 40 * 0.309],  # Right top point
        [pos[0] + 40, pos[1] - 40 * 0.309],  # Right point
        [pos[0] + 40 * 0.363, pos[1] + 40 * 0.118],  # Right bottom point
        [pos[0] + 40 * 0.5878, pos[1] + 40],  # Bottom right point
        [pos[0], pos[1] + 40 * 0.382],  # Bottom point
        [pos[0] - 40 * 0.5878, pos[1] + 40],  # Bottom left point
        [pos[0] - 40 * 0.363, pos[1] + 40 * 0.118],  # Left bottom point
        [pos[0] - 40, pos[1] - 40 * 0.309],  # Left point
        [pos[0] - 40 * 0.2245, pos[1] - 40 * 0.309],  # Left top point
    ], np.int32)

    cv2.fillPoly(image2, [pts], color=(0, 0, 255))  # 绘制实心红色五角星


cv2.imwrite('five_star_filled.jpg', image2)

